local MIN_CHANCE = 0
local MAX_CHANCE = 1
local DEFAULT_CHANCE = 0.02

local function onchance(self, chance)

end

local AttackPoison = Class(function(self, inst) 
    self.inst = inst
    self.default = 0
    self.chance = DEFAULT_CHANCE
    self.duration = 10
    self.extra_chance = 0
    self.force = false
    self.fn = nil
end,
nil,
{
    chance = onchance,
})

function AttackPoison:SetPoisonFn(fn)
    self.fn = fn
end

function AttackPoison:SetChance(chance)
    self.chance = chance
end

function AttackPoison:GetFinalChance()
    return self.force and 1 or (self.chance + self.extra_chance)
end

function AttackPoison:Effect(target)
    if target == nil then
        return false
    end
    if target.components.poison == nil then
        return false
    end
    if math.random() < self:GetFinalChance() then
        local damage = (target.components.health and target.components.health.maxhealth or 1) * 0.01
        damage = math.max(1, damage)
        local duration = nil 
        if target:HasTag("player") then
            damage = damage * 2 
            duration = 30
        end
        target.components.poison:Poison(damage, duration, self.inst)
        if self.fn then
            self.fn(self.inst)
        end
        return true
    end
    return false
end

function AttackPoison:OnRemoveFromEntity()
    
end

return AttackPoison